import Cookies from 'js-cookie'
import store from '@/store'

/**
 * 权限
 * @param {*} key
 */
export function hasPermission (key) {
  return window.SITE_CONFIG['permissions'].indexOf(key) !== -1 || false
}

/**
 * 获取字典数据列表
 * @param dictType  字典类型
 */
export function getDictDataList (dictType) {
  const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
  if (type) {
    return type.dataList
  } else {
    return []
  }
}

/**
 * 获取字典名称
 * @param dictType  字典类型
 * @param dictValue  字典值
 */
export function getDictLabel (dictType, dictValue) {
  const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
  if (type) {
    const val = type.dataList.find((element) => (element.dictValue === dictValue + ''))
    if (val) {
      return val.dictLabel
    } else {
      return dictValue
    }
  } else {
    return dictValue
  }
}
/**
 * 获取字典值
 * @param dictType  字典类型
 * @param dictLabel  字典值
 */
export function getDictValue (dictType, dictLabel) {
  const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
  if (type) {
    const val = type.dataList.find((element) => (element.dictLabel === dictLabel + ''))
    if (val) {
      return val.dictValue
    } else {
      return dictLabel
    }
  } else {
    return dictLabel
  }
}

export function getDictValue2 (dictType, dictLabel) {
  dictLabel =  dictLabel.replace(/\d+/g, '')
  const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
  if (type) {
    const val = type.dataList.find((element) => (element.dictLabel === dictLabel + ''))
    if (val) {
      return val.dictValue
    } else {
      return dictLabel
    }
  } else {
    return dictLabel
  }
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  store.commit('resetStore')
  Cookies.remove('token')
  window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = false
}

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 获取svg图标(id)列表
 */
export function getIconList () {
  var res = []
  var list = document.querySelectorAll('svg symbol')
  for (var i = 0; i < list.length; i++) {
    res.push(list[i].id)
  }

  return res
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'pid') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) {
      res.push(data[k])
      continue
    }
    if (!temp[data[k][pid]]['children']) {
      temp[data[k][pid]]['children'] = []
    }
    temp[data[k][pid]]['children'].push(data[k])
    data[k]['_level'] = (temp[data[k][pid]]._level || 0) + 1
  }
  return res
}
/**
 * 时间戳格式化函数
 * @param number
 * @param {string} format 要格式化的时间 默认为当前时间
 */
export function formatdate(number, format) {
  if (!format) {
    format = 'Y/M/D h:m:s'
  }
  if (!number || number === '') {
    return '';
  }
  const formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  const returnArr = [];

  const date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (const i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }

  //数据转化
  function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }
  return format;
}
/**
 * 格式化数字函数保留两位小数及千分位并返回
 * @param numStr
 */
export function formatNumber(numStr,noRetain) {
  if (!isNaN(numStr) && !isNaN(parseFloat(numStr))) {
    numStr = !noRetain? parseFloat(numStr).toFixed(2):numStr; // 先将数值四舍五入到两位小数
    numStr = numStr.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); // 添加千分位逗号
    // return numStr.toLocaleString('zh-CN', { style: 'currency', currency: 'CNY' }); // 转换成千分位形式显示单位;
    return numStr;
  } else {
    return numStr;
  }
}
